var utils = require("../lib/Utils");

exports.suppList=function (req,res,next) {
    utils.render(req,res,"suppList",{});
}


exports.getSuppList = function (req,res,next) {
    var param = {
        limit:req.body.limit,
        offset:req.body.offset
    }

    utils.httpPost("/supp/getAllSupp",param,function (data) {
        if(data.code === "0"){
            res.status(200).end(JSON.stringify({code:"0",msg:"获取成功",rows:data.data.suppList,total:data.data.cnt}));
        }else {
            res.status(200).end(JSON.stringify({code:"-1",msg:"获取失败"}));
        }
    })
}


exports.addSupp = function (req,res,next) {
    var id = req.query.id;
    if(id == '' || id == undefined) {
        utils.render(req, res, "addSupp", {});
    }else {
        utils.httpPost("/supp/getSuppInfo",{id:id},function (data) {
            if(data.code === "0"){
                var supp = data.data;
                utils.render(req,res,"addSupp",{supp:JSON.stringify(supp)});
            }else {
                utils.render(req,res,"addSupp",{});
            }
        })
    }
}

exports.addSuppInfo = function (req,res,next) {
    var param = {
        suppName:req.body.suppName,
        suppChargeRatio:req.body.suppChargeRatio,
        suppUserWxId:req.body.suppWxId,
        suppUserName:req.body.suppUserName,
        suppTelephone:req.body.suppTelephone,
        suppAddress:req.body.suppAddress,
        createUser:req.session.userid,
        updUser:req.session.userid,
        id:req.body.suppId
    }

    if(req.body.suppId == 0){
        utils.httpPost("/supp/insSuppInfo",param,function (data) {
            if(data.code === "0"){
                res.status(200).end(JSON.stringify({code:"0",msg:"添加成功",data:data.data}));
            }else {
                res.status(200).end(JSON.stringify({code:"-1",msg:"添加失败"}));
            }
        });
    }else {
        utils.httpPost("/supp/updSuppInfo",param,function (data) {
            if(data.code === "0"){
                res.status(200).end(JSON.stringify({code:"0",msg:"修改成功",data:data.data}));
            }else {
                res.status(200).end(JSON.stringify({code:"-1",msg:"修改失败"}));
            }
        });
    }
}

exports.delSupp = function (req,res,next) {
    var ids = JSON.parse(req.body.ids);
    var param = {
        ids:ids.join(','),
        updUser:req.session.userid
    }

    utils.httpPost("/supp/delSupp",param,function (data) {
        if(data.code === "0"){
            console.log(data);
            res.status(200).end(JSON.stringify({code:"0",msg:"删除成功"}));
        }else {
            res.status(200).end(JSON.stringify({code:"-1",msg:"删除成功"}));
        }
    })
}

exports.checkSupp = function (req,res,next) {
    var ids = JSON.parse(req.body.ids);
    var param = {
        ids:ids.join(','),
        updUser:req.session.userid
    }

    utils.httpPost("/supp/checkSupp",param,function (data) {
        if(data.code === "0"){
            console.log(data);
            res.status(200).end(JSON.stringify({code:"0",msg:"审核成功"}));
        }else {
            res.status(200).end(JSON.stringify({code:"-1",msg:"审核成功"}));
        }
    })
}